package com.topisv.tms.repository.work;

import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.topisv.tms.entity.work.WorkTaskItem;
import com.topisv.tms.service.work.WorkTaskService;

/**
 * 作业任务_拣货信息明细_DAO
 * @author ch
 *
 */
public abstract interface WorkTaskItemDao extends PagingAndSortingRepository<WorkTaskItem, Long>, JpaSpecificationExecutor<WorkTaskItem>
{

	@Query("select t from WorkTaskItem t where t.taskCode=? order by id")
	List<WorkTaskItem> loadByTaskCode(String taskCode);

	@Query("select t from WorkTaskItem t where t.taskCode=? and  t.pickStatus !='picked'  ")
	List<WorkTaskItem> loadUnPickItemByTaskCode(String taskCode);
	
	@Query("select t from WorkTaskItem t where t.batchCode=? and (  t.pickStatus='unpicked' or t.pickStatus='picking' )  ")
	List<WorkTaskItem> loadUnPickItemByBatchCode(String batchCode);

	WorkTaskItem findByTaskCodeAndRankno(String taskCode,int rankno);
	
	List<WorkTaskItem> findByTaskCodeAndSku(String taskCode,String sku);
	
	List<WorkTaskItem> findByTaskCode(String taskCode);

	@Query("select t from WorkTaskItem t where t.orderno=? order by t.stockCode")
	List<WorkTaskItem> loadByOrderno(String orderno);

	
	/**
	 * 更新拣货数量和状态,
	 * 注：此方法返回始终为0，放弃使用。
	 * @see WorkTaskService#updateTaskItemForPickNew(int, String, int, long)
	 * 
	 * add 20160621 
	 * @param pickNum
	 * @param status
	 * @param taskId
	 * @return int
	 */
	@Deprecated
	@Modifying
	@Query("update WorkTaskItem set pickedNum=pickedNum + ?1, pickStatus=?2 , version = version + 1 where id=?4  and version=?3 and num >= (pickedNum + ?1)")
	int updateTaskItemForPick(int pickNum, String status, int version, long taskItemId);

}